package com.tuanbiao.presenter.setting;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.alibaba.fastjson.JSONObject;
import com.hzjq.core.bean.Param;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.SettingController;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/28 10:01
 */
public class SettingControlPanelPresenter extends BasePresenter<SettingController.Model, SettingController.ControlPanelView> implements SettingController.ControlPanelPresenter {
    public SettingControlPanelPresenter(SettingController.Model model, SettingController.ControlPanelView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    @Override
    public void getSettingParams() {
        mModel.getParams(param -> mRootView.showSettingParams(param));
    }

    @Override
    public void setSettingParams(String connectVoltage, String chargeVoltage, String queryVoltage, String chargeAdc, String outageDelay
            , String groupingCount, String groupingInterval, String singleCheck, String reservedOne, String reservedTwo
            , String reservedThree, String reservedFour) {
        if (TextUtils.isEmpty(connectVoltage)) {
            mRootView.showMessage(R.string.setting_control_panel_connect_voltage_hint);
            return;
        }
        if (TextUtils.isEmpty(chargeVoltage)) {
            mRootView.showMessage(R.string.setting_control_panel_charge_voltage_hint);
            return;
        }
        if (TextUtils.isEmpty(queryVoltage)) {
            mRootView.showMessage(R.string.setting_control_panel_query_voltage_hint);
            return;
        }
        if (TextUtils.isEmpty(chargeAdc)) {
            mRootView.showMessage(R.string.setting_control_panel_charge_adc_hint);
            return;
        }
        if (TextUtils.isEmpty(outageDelay)) {
            mRootView.showMessage(R.string.setting_control_panel_outage_delay_hint);
            return;
        }
        if (TextUtils.isEmpty(groupingCount)) {
            mRootView.showMessage(R.string.setting_control_panel_grouping_count_hint);
            return;
        }
        if (TextUtils.isEmpty(groupingInterval)) {
            mRootView.showMessage(R.string.setting_control_panel_grouping_interval_hint);
            return;
        }

        if (TextUtils.isEmpty(singleCheck)) {
            mRootView.showMessage(R.string.setting_control_panel_single_check_hint);
            return;
        }
        if (TextUtils.isEmpty(reservedOne)) {
            mRootView.showMessage(R.string.setting_control_panel_reserved_one_hint);
            return;
        }
        if (TextUtils.isEmpty(reservedTwo)) {
            mRootView.showMessage(R.string.setting_control_panel_reserved_two_hint);
            return;
        }
        if (TextUtils.isEmpty(reservedThree)) {
            mRootView.showMessage(R.string.setting_control_panel_reserved_three_hint);
            return;
        }
        if (TextUtils.isEmpty(reservedFour)) {
            mRootView.showMessage(R.string.setting_control_panel_reserved_four_hint);
            return;
        }


        int connectVoltageInt = Integer.parseInt(connectVoltage);
        int chargeVoltageInt = Integer.parseInt(chargeVoltage);
        int queryVoltageInt = Integer.parseInt(queryVoltage);
        int chargeAdcInt = Integer.parseInt(chargeAdc);
        int outageDelayInt = Integer.parseInt(outageDelay);
        int groupingCountInt = Integer.parseInt(groupingCount);
        int groupingIntervalInt = Integer.parseInt(groupingInterval);
        int singleCheckInt = Integer.parseInt(singleCheck);
        int reservedOneInt = Integer.parseInt(reservedOne);
        int reservedTwoInt = Integer.parseInt(reservedTwo);
        int reservedThreeInt = Integer.parseInt(reservedThree);
        int reservedFourInt = Integer.parseInt(reservedFour);
        if (connectVoltageInt > 255 || chargeVoltageInt > 255 || queryVoltageInt > 255 || chargeAdcInt > 255
                || outageDelayInt > 255 || groupingCountInt > 255 || groupingIntervalInt > 255 || singleCheckInt > 255
                || reservedOneInt > 255 || reservedTwoInt > 255 || reservedThreeInt > 255 || reservedFourInt > 255) {
            mRootView.showMessage("所设数值不能大于255");
            return;
        }



        Param param = new Param();
        param.setSignalVoltage(connectVoltageInt);
        param.setChargeVoltage(chargeVoltageInt);
        param.setQueryVoltage(queryVoltageInt);
        param.setChargeAdc(chargeAdcInt);
        param.setBlastCutPower(outageDelayInt);
        param.setGroupChargeCount(groupingCountInt);
        param.setGroupChargeSleepTime(groupingIntervalInt);
        //与FUXIN一致
        param.setBlastAdc(reservedOneInt);
        param.setReserve1(singleCheckInt);
        param.setReserve2(reservedTwoInt);
        param.setReserve3(reservedThreeInt);
        param.setReserve4(reservedFourInt);

        Timber.i(JSONObject.toJSONString(param));

        mModel.updateSettingParam(param, aBoolean -> {
            if (aBoolean) mRootView.showMessage("更新成功");
            else mRootView.showMessage("更新失败");
        });
    }
}
